package com.jiemengshi.server.service;

import com.jiemengshi.server.mapper.AttachmentMapper;
import com.jiemengshi.server.model.Attachment;
import com.jiemengshi.server.qiniu.QiniuCallbackObj;
import com.jiemengshi.server.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by MingQi on 2017/10/28.
 */
@Service
public class QiniuService {
    @Autowired
    private AttachmentMapper attachmentMapper;
    
    public Attachment saveQiniuAttachment(QiniuCallbackObj callback){
        Attachment attachment = new Attachment();

        attachment.setFileName(callback.getFname());
        attachment.setFileSize(callback.getFsize());
        attachment.setFileType(callback.getMimeType());

        attachment.setUserId(callback.getUserId());
        attachment.setUserAdminId(callback.getUserAdminId());
        attachment.setQiniuKey(callback.getKey());
        attachment.setCreateTime(DateUtil.getCurrentTime());

        if(attachmentMapper.insertSelective(attachment) > 0)
            return attachment;
        return null;
    }

    public Attachment getAnnouncementImage() {
        return attachmentMapper.selectOneByTime();
    }

    public Attachment getAttachmentById(int id) {
        return attachmentMapper.selectByPrimaryKey(id);
    }
}
